const state = {
	talks: [],
	userSelectTalk: {},
	total: 0,
}
const mutations = {
	SET_TALKS(state, data) {
		state.talks = data;
	},
	SET_TOTAL(state, total) {
		state.total = total;
	},
	SET_SETUSERSELECTTALK(state, talk) {
		state.userSelectTalk = talk;
	},
	LIKE_TALK(state){
		state.userSelectTalk.likes += 1;
	}
}
var orderMap = {
	"默认排序": 'id DESC',
	'时间顺序': 'creationTime ASC',
	'时间倒序': 'creationTime DESC'
}
const actions = {
	async getTalks({ commit }, data) {
		data.orderBy = orderMap[data.orderBy];
		let result = await uni.$u.http.get('/talk/get/all', { params: { ...data } })
		if (result.data.code === 200) {
			commit('SET_TALKS', result.data.talks);
			commit('SET_TOTAL', result.data.total);
		}
		return result.data.code
	},
	async getMoreTalks({ commit }, data) {
		data.orderBy = orderMap[data.orderBy];
		let result = await uni.$u.http.get('/talk/get/all', { params: { ...data } })
		if (result.data.code === 200) {
			commit('SET_MORETALKS', result.data.talks);
		}
		return result.data.code
	},
	// 点赞
	async likeTalk({ commit }, data) {
		let result = await uni.$u.http.get('/talk/like', { params: { ...data } });
		if(result.data.code === 200) commit('LIKE_TALK')
		return result.data.code;
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
